<?php
// Specify the root path of core script
Yii::app()->getClientScript()->setCoreScriptUrl(Yii::app()->baseUrl.'/assets/637def09');
// Set the user login's url
Yii::app()->user->loginUrl = array('users/signin');

Yii::app()->getDb()->createCommand('SET sql_mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"')->execute();

/** ================
 *  Common functions
 *  ================
 */

/**
 * Check if there is a post request
 * @return boolean
 */
function isPost() {
	return Yii::app()->request->isPostRequest;
}

/**
 * Check if there is a AJAX request
 * @return unknown_type
 */
function isAjax() {
	return Yii::app()->request->isPostRequest;
}

/**
 * Generate a random string by MD5
 * @return string
 */
function randStr() {
	return md5(uniqid().$_SERVER['REMOTE_ADDR'].time());
}

/**
 * Get the extension of a path
 * @param $path
 * @param $includingDot Need to add a dot or not
 * @return string
 */
function getExt($path, $includingDot = false) {
	$ext = pathinfo($path, PATHINFO_EXTENSION);
	$ext = $includingDot ? '.'.$ext : $ext;
	return $ext;
}

/**
 * Describe the rule convert from UNIX timestamp to date string
 * @param $timestamp
 * @return string
 */
function toDate($timestamp) {
	return date(Yii::app()->params->dateFormat, $timestamp);
}

/**
 * Truncate the string to specified length, just for UTF-8 encoding
 * @param $str
 * @param $length
 * @return string
 */
function truncStr($str, $length, $etc = '') {
//	if($length > strlen($str)) {
//		return $str;
//	}
//	$step = 3;
//	$sbcRegExp = '/^[\x{4e00}-\x{9fa5}]+$/u';
// 
//	$tmp1 = substr($str, 0, $length);
//	$tmp2 = '';
//	for($i = 0; $i<3; $i++) {
//		$tmp2 = substr($tmp1, $length-$step-$i, 3);
//		if(preg_match($sbcRegExp, $tmp2)) {
//			$str = substr($str, 0, $length-$step-$i+3);
//			break;
//		} else if(ord(substr($tmp1, $length-$i, 1) < 128)) {
//			$str = substr($str, 0, $length-$i);
//		}
//	}
	$length = (int)$length/3;
	if(mb_strlen($str, 'UTF-8') < $length) {
		return $str;
	}
	$str = mb_substr($str, 0, $length, 'UTF-8');
	return $etc ? $str.$etc : $str;
}

/**
 * Returns the time of range between the specified's one and the current
 * @param $time
 * @return unknown_type
 */
function fromNow($time) {
	$range = time()-$time;
	if($range > 3600*24*30) {
		return date('Y年n月j日H时i分', $time);
	} else if($range < 300) {
		return '刚才';
	}
	$day = intval($range/3600/24);
	$hour = ($range/3600)%24;
	$minute = ($range/60)%60;
	$text = '';
	if($day !== 0) {
		$text .= '&thinsp;'.$day.'&thinsp;天';
	}
	if($hour !== 0) {
		if($day <= 3) {
			$text .= '&thinsp;'.$hour.'&thinsp;小时';
		}
	} else if($minute !== 0) {
		$text .= '&thinsp;'.$minute.'&thinsp;分钟';
	}
	return $text.'之前';
}

/**
 * Initialize this project's some JavaScript global variables
 * @return void
 */
function initJsVar() {
	$baseUrl = Yii::app()->baseUrl;

	$js = <<<EOT
var baseUrl = "$baseUrl";
EOT;
	Yii::app()->clientScript->registerScript('init', $js, CClientScript::POS_HEAD);
}

function sendMail($sendTo, $receiverName, $subject, $content) {
	Yii::import('application.extensions.mailer.EMailer');
	$mailer = new EMailer;
	$mailer->IsSMTP(); // 使用SMTP
	$mailer->SMTPSecure = "ssl"; // 设置连接服务器前缀
	$mailer->Host = 'smtp.gmail.com';      // GMAIL的SMTP
	$mailer->Port = 465;                   // GMAIL的SMTP端口号
	$mailer->SMTPAuth = true; // 开启SMTP认证
	$mailer->Username = Yii::app()->params->smtpUsername; // GMAIL用户名
	$mailer->Password = Yii::app()->params->smtpPassword; // GMAIL密码
	$email = $sendTo; // 收件人
	$name = $receiverName; // 收件人名称
	$mailer->From = Yii::app()->params->adminEmail;
	$mailer->FromName = "广州心理咨询网";
	$mailer->AddAddress($email, $name);
	$mailer->AddReplyTo(Yii::app()->params->adminEmail, "慧苑心理咨询");
	$mailer->WordWrap = 50; // 设定 word wrap
	$mailer->IsHTML(true); // 以HTML发送
	$mailer->CharSet = Yii::app()->params->emailBodyCharset;
	$mailer->Subject = $subject;
	$mailer->Body = $content; //HTML Body
	$mailer->AltBody = strip_tags($content); //纯文字时的Body
	return $mailer->Send();
}
